package com.shiro.test.mvc.service.impl;

import com.shiro.test.mvc.dao.IBaseDao;
import com.shiro.test.mvc.filter.MyShiroFilterFactoryBean;
import com.shiro.test.mvc.mapper.MenuMapper;
import com.shiro.test.mvc.mapper.RoleMapper;
import com.shiro.test.mvc.pojo.MenuPermission;
import com.shiro.test.mvc.pojo.Role;
import com.shiro.test.mvc.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020/11/26 0026.
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired  //更新用户权限的操作
    private MyShiroFilterFactoryBean shiroFilterFactoryBean;

    @Override
    public IBaseDao<Role> getDao() {
        return roleMapper;
    }


    @Override
    public List<Role> getRoleList() {
        return roleMapper.getRoleList();
    }

    @Override
    public void addRoleMenu(int roleId, int[] menuIds) {
        roleMapper.deleteRoleMenu(roleId);
        Map<String,Integer> param = new HashMap<>();
        param.put("roleId",roleId);
        for (int menuId : menuIds) {
            param.put("menuId",menuId);
            roleMapper.addRoleMenu(param);
        }
        //调用更改用户权限的方法
        shiroFilterFactoryBean.update();
    }

    @Override
    public List<Integer> getRoleMenuIds(int roleId) {
        return roleMapper.getRoleMenuIds(roleId);
    }

}
